import { DictTag } from "@/components/common/DictTag";
import { dealDataExcept, dealDataRide } from "@/utils/common";
import { goodsCodeRend, goodsNameRend } from "@/utils/tableColumns";
import { ElTag } from "element-plus";
import { h } from "vue";
export function useColumns() {
  // 订单表格
  const orderColumns: TableColumnList = [
    {
      label: "订单类型",
      prop: "orderType",
      minWidth: 100,
      formatter: ({ storeOrderCode }) =>
        storeOrderCode.indexOf("KD") != -1 ? "跨店销售" : "门店采购"
    },
    {
      label: "业务订单编号",
      prop: "storeOrderCode",
      minWidth: 145
    },
    {
      label: "状态",
      prop: "status",
      minWidth: 100,
      cellRenderer: ({ row }) =>
        row.storeOrderCode.indexOf("KD") != -1
          ? getKdOrderStatus(row.status)
          : getOrderStatus(row.status)
    },
    {
      label: "总金额",
      prop: "totalPrice",
      minWidth: 80,
      formatter: ({ totalPrice }) => dealDataExcept(totalPrice)
    },
    {
      label: "商品总数量",
      prop: "totalCommodities",
      minWidth: 100
    },
    {
      label: "到货仓库",
      prop: "depositoryName",
      minWidth: 100
    }
  ];

  // 获取订单状态 primary success danger
  const getOrderStatus = status => {
    if (status == null) {
      return "--";
    }
    // 	采购订单状态 0 待审核 1 发货中 2部分发货 3 已发货 4 已完成 5 已驳回 6 已取消 7 已关闭
    let statusMap = {
      0: { text: "待审核", type: "warning" },
      1: { text: "发货中", type: "primary" },
      2: { text: "部分发货", type: "primary" },
      3: { text: "已发货", type: "primary" },
      4: { text: "已完成", type: "success" },
      5: { text: "已驳回", type: "danger" },
      6: { text: "已取消", type: "info" },
      7: { text: "已关闭", type: "danger" }
    };
    let obj = statusMap[status];
    if (!obj) {
      return "--";
    }
    return h(
      ElTag,
      {
        type: obj.type
      },
      () => obj.text
    );
  };

  // 获取跨店订单状态
  const getKdOrderStatus = status => {
    if (status == null) {
      return "--";
    }
    // 订单状态 0 待审核 1 已驳回 2 待确认 3 已完成 4 已取消
    let statusMap = {
      0: { text: "待审核", type: "warning" },
      2: { text: "待确认", type: "warning" },
      3: { text: "已完成", type: "success" },
      1: { text: "已驳回", type: "danger" },
      4: { text: "已取消", type: "danger" }
    };
    let obj = statusMap[status];
    if (!obj) {
      return "--";
    }
    return h(
      ElTag,
      {
        type: obj.type
      },
      () => obj.text
    );
  };

  // 商品明细
  const goodsColumns: TableColumnList = [
    {
      label: "商品名称/规格名称",
      prop: "goodsName",
      minWidth: 180,
      align: "left",
      fixed: "left",
      cellRenderer: goodsNameRend
    },
    {
      label: "商品编码/规格编码",
      prop: "goodsCode",
      align: "left",
      minWidth: 205,
      cellRenderer: goodsCodeRend
    },
    {
      label: "商品条码",
      prop: "barCode",
      minWidth: 120,
      formatter: row => row.barCode || "--"
    },
    {
      label: "商品类型",
      prop: "goodsType",
      minWidth: 85,
      cellRenderer: ({ row }) => (
        <DictTag dict-type="PRODUCT_TYPE" dict-value={row.goodsType} />
      )
    },
    {
      label: "商品数量",
      prop: "goodsAmount",
      minWidth: 80
    },
    {
      label: "商品单价",
      prop: "buyPrice",
      minWidth: 80,
      formatter: ({ buyPrice }) => dealDataExcept(buyPrice)
    },
    {
      label: "入库金额",
      prop: "buyPrice",
      minWidth: 80,
      formatter: row =>
        dealDataExcept(dealDataRide(row.stockCount || 0, row.buyPrice)) || "--"
    },
    {
      label: "已入库数量",
      prop: "storeAmount",
      minWidth: 100
    },
    {
      label: "待入库数量",
      prop: "stayStoreAmount",
      minWidth: 100,
      slot: "stayStoreAmount"
    },
    {
      label: "入库数量",
      prop: "stockCount",
      minWidth: 145,
      cellRenderer: ({ row }) => (
        <el-input-number
          v-model={row.stockCount}
          class="!w-[130px]"
          placeholder="请输入"
          precision={0}
          min={1}
          max={row.goodsAmount || 99999}
        />
      )
    }
  ];

  return {
    orderColumns,
    goodsColumns
  };
}
